package com.mywang.weitravel.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.mywang.weitravel.R;
import com.mywang.weitravel.model.StudyBean;
import com.mywang.weitravel.modules.study.StudyDetailActivity;
import com.mywang.weitravel.util.ImageLoader;

import java.util.List;

/**
 * Created by wangmingyun on 2019/12/11.
 */
public class StudyListAdapter extends RecyclerView.Adapter<StudyListAdapter.StudyHolder> {

    private Context mContext;

    private List<StudyBean> mDatas;

    public StudyListAdapter(Context mContext, List<StudyBean> data) {
        this.mContext = mContext;
        this.mDatas = data;
    }

    @NonNull
    @Override
    public StudyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_study_list, parent, false);
        return new StudyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StudyHolder holder, int position) {
        StudyBean bean = mDatas.get(position);
        ImageLoader.getInstance().displayImage(mContext, bean.getFilepath(), holder.ivPic);
        holder.tvTitle.setText(bean.getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getUrl() == null || bean.getUrl().trim().equals("")) {
                    ToastUtils.showShort("链接出错");
                    return;
                }
                Intent intent = new Intent(mContext, StudyDetailActivity.class);
                intent.putExtra("url", bean.getUrl());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class StudyHolder extends RecyclerView.ViewHolder {
        ImageView ivPic;
        TextView tvTitle;

        public StudyHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.iv_pic);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }
}
